package com.abel.util;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;

public class PDFTableTester {

	public void createPDF(OutputStream outputStream) {

		try {
			
			//step1 create a placeholder for the final pdf
			Document mergedPDF = new Document();
			PdfCopy writer = new PdfCopy(mergedPDF, outputStream);
			mergedPDF.open();

			
			//step 2 get the template
			ByteArrayOutputStream page1Stream = new ByteArrayOutputStream();
			PdfStamper stamp = null;
			PdfReader applicationTemplate = new PdfReader("C:/works/my/testbed/Application_Template.pdf");
			stamp = new PdfStamper(applicationTemplate, page1Stream);
		
			//set values to the template
			stamp.getAcroFields().setField("first_name", "joseph");
			stamp.getAcroFields().setField("address_line_1", "6109 MacArthur dr");
			stamp.setFormFlattening(true);
			stamp.close();

			
			//step 3 adding the created page1 pdf to the final pdf
			PdfReader completedPDF = new PdfReader(page1Stream.toByteArray());

			//first we add the pages
			int page1count = completedPDF.getNumberOfPages();
			for (int i = 0; i < page1count; ++i) {
				PdfImportedPage page = writer.getImportedPage(completedPDF, i + 1);
				writer.addPage(page);
			}
			
			//then we add the form 
			PRAcroForm completedform = completedPDF.getAcroForm();
			if (completedform != null) {
				writer.copyAcroForm(completedPDF);
			}
			
			//step 4 add table to the pdf
			//FileOutputStream tablePage  = new FileOutputStream("C:/works/my/testbed/test.pdf");
			ByteArrayOutputStream tablePage = new ByteArrayOutputStream();
			Document document = new Document();
			PdfWriter tempwriter = PdfWriter.getInstance(document, tablePage);
			document.open();
			document.newPage();
			tempwriter.setPageEmpty(false);
			
			//append table
			PdfPTable table = new PdfPTable(2);
			PdfPCell cell = new PdfPCell(new Paragraph("column span 2"));
			cell.setColspan(2);
			table.addCell(cell);
			
			int i = 0;
			while (i < 100) {
				table.addCell(new Integer(++i).toString());
			}
			document.add(table);
			document.close();
			
			//add the table pdf to the final pdf
			PdfReader completedTablePDF = new PdfReader(tablePage.toByteArray());
			int tablepdfpagecount = completedTablePDF.getNumberOfPages();
			
			for (int j = 0; j < tablepdfpagecount; ++j) {
				
				PdfImportedPage page = writer.getImportedPage(completedTablePDF, j + 1);
				writer.addPage(page);
			
			}

			mergedPDF.close();
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
		catch (DocumentException e) {
			e.printStackTrace();
		}
		
		
	}
	
	public static void main(String[] args) {
		PDFTableTester pDFTableTester =  new PDFTableTester();
		String outputFileName = "C:/works/my/testbed/Application_" + Long.toString(new Date().getTime()) + ".pdf";
		try {

			OutputStream pdfStream = new FileOutputStream(outputFileName);
			pDFTableTester.createPDF(pdfStream);
		
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	
}
